package com.cqu.hospitalsystem.controller;

import com.cqu.hospitalsystem.Vo.CheckResult;
import com.cqu.hospitalsystem.entity.ItemListRelation;
import com.cqu.hospitalsystem.service.ItemListRelationService;
import com.cqu.hospitalsystem.utils.result.DataResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.Map;

@RestController
@RequestMapping("itemListRelation")
@Tag(name = "ItemListRelationController", description = "项目列表关系管理")
public class ItemListRelationController {
    /**
     * 服务对象
     */
    @Resource
    private ItemListRelationService itemListRelationService;

    /**
     * 分页查询
     *
     * @param itemListRelation 筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @GetMapping
    @Operation(summary = "分页查询项目列表关系", description = "根据筛选条件和分页信息查询项目列表关系")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "400", description = "查询失败")
    })
    public ResponseEntity<Page<ItemListRelation>> queryByPage(ItemListRelation itemListRelation, PageRequest pageRequest) {
        return ResponseEntity.ok(this.itemListRelationService.queryByPage(itemListRelation, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @Operation(summary = "根据ID查询项目列表关系", description = "根据项目列表关系ID查询单个项目列表关系")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "项目列表关系不存在")
    })
    public ResponseEntity<ItemListRelation> queryById(@PathVariable("id") @Parameter(description = "项目列表关系ID") Long id) {
        return ResponseEntity.ok(this.itemListRelationService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param itemListRelation 实体
     * @return 新增结果
     */
    @PostMapping
    @Operation(summary = "新增项目列表关系", description = "创建新的项目列表关系")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "201", description = "创建成功"),
            @ApiResponse(responseCode = "400", description = "创建失败")
    })
    public ResponseEntity<ItemListRelation> add(@RequestBody @Parameter(description = "项目列表关系对象") ItemListRelation itemListRelation) {
        return ResponseEntity.ok(this.itemListRelationService.insert(itemListRelation));
    }

    /**
     * 编辑数据
     *
     * @param itemListRelation 实体
     * @return 编辑结果
     */
    @PutMapping
    @Operation(summary = "编辑项目列表关系", description = "更新项目列表关系信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "更新成功"),
            @ApiResponse(responseCode = "400", description = "更新失败")
    })
    public ResponseEntity<ItemListRelation> edit(@RequestBody @Parameter(description = "项目列表关系对象") ItemListRelation itemListRelation) {
        return ResponseEntity.ok(this.itemListRelationService.update(itemListRelation));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    @Operation(summary = "删除项目列表关系", description = "根据项目列表关系ID删除项目列表关系")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "删除成功"),
            @ApiResponse(responseCode = "400", description = "删除失败")
    })
    public ResponseEntity<Boolean> deleteById(@Parameter(description = "项目列表关系ID") Long id) {
        return ResponseEntity.ok(this.itemListRelationService.deleteById(id));
    }

    @PostMapping("setCheckResult")
    @Operation(summary = "上传检查结果", description = "根据项目列表关系ID上传检查结果，包括描述和结果图片")
    public DataResult setCheckResult(
            @RequestParam("itemListId") String itemListId,
            @RequestParam("resultText") String resultText,
            @RequestParam("resultImg") MultipartFile resultImg) {
        CheckResult checkResult = new CheckResult();
        checkResult.setItemListId(Long.valueOf(itemListId));
        checkResult.setResultText(resultText);
        checkResult.setResultImg(resultImg);
        return this.itemListRelationService.setCheckResult(checkResult);
    }

    @PostMapping("getCheckResult")
    @Operation(summary = "查看检查结果", description = "通过检查项目-列表ID查看检查结果")
    public DataResult getCheckResult(
            @RequestBody Map<String, Object> map
    ){
        String itemListId = (String) map.get("itemListId");
        return this.itemListRelationService.getCheckResult(Long.valueOf(itemListId));
    }
}